<?php
/**
 * @title 845. 数组中的最长山脉 - 动态规划
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[] $A
     * @return Integer
     */
    function longestMountain($A) {
        /**
         * @since 2020-10-26 长度
         */
        $len = count($A);

        /**
         * @since 2020-10-26 升序, 降序长度
         */
        $maxlen = 0;
        $asclen = 1;
        $desclen = 0;

        /**
         * @since 2020-10-26 迭代
         */
        for($i=1; $i<$len; $i++){
            if($A[$i] > $A[$i-1]){
                /**
                 * @since 2020-10-26 升序
                 * @since 2020-10-26 当前升序段
                 */
                if($desclen == 0){
                    $asclen++;
                } else {
                    /**
                     * @since 2020-10-26 当前降序段
                     */
                    $maxlen = max($maxlen, $asclen+$desclen);

                    /**
                     * @since 2020-10-26 重置
                     */
                    $asclen = 2;
                    $desclen = 0;
                }
            } elseif($A[$i] < $A[$i-1]) {
                /**
                 * @since 2020-10-26 降序
                 * @since 2020-10-26 当前降序段
                 */
                if($asclen > 1){
                    $desclen++;
                } elseif($asclen == 1) {
                    /**
                     * @since 2020-10-26 无升序段, 维持
                     */
                }
            } else {
                /**
                 * @since 2020-10-26 相同
                 */
                if($desclen > 0){
                    $maxlen = max($maxlen, $asclen+$desclen);
                } else {}

                /**
                 * @since 2020-10-26 重置
                 */
                $asclen = 1;
                $desclen = 0;
            }

            // echo "i={$i}, asclen={$asclen}, desclen={$desclen}, len={$maxlen}", PHP_EOL;
        }

        /**
         * @since 2020-10-26 收官
         */
        if($desclen > 0){
            $maxlen = max($maxlen, $asclen+$desclen);
        } else {}

        /**
         * @return
         */
        return $maxlen;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.25.php
 */
$datas = [
    [[40,51,29,19,50,25]], // 4
    [[0,1,2,3,4,5,4,3,2,1,0]], // 11
    [[2,1,4,7,3,2,5]], // 5
    [[2,2,2]], // 0
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->longestMountain(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
